#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "wn_device.h"
#include "wn_protocol.h"
#include "../emu_common.h"
#include <rtthread.h>


static void dump_buffer(void *buffer, int count)
{
    int i = 0;
    unsigned char *tmp = (unsigned char *)buffer;

    for (i=0; i<count; i++) {
        if ( (i != 0) && (i%16 == 0)) {
            printf("\n");
        }
        printf("%02x:", tmp[i]);
    }
    printf("\n");
}


void init_wn_task(void)
{
   // thread_create("uart_wn_task", 2048, uart_thread_func, NULL);
}
/* 用于接收消息的信号量 */
static struct rt_semaphore rx_sem;
static rt_device_t serial;

/* 接收数据回调函数 */
static rt_err_t uart_rx_ind(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    if (size > 0)
    {
        serial_t *serial = dev->user_data;
        if(serial != NULL)
            rt_sem_release(&serial->rx_sem);
    }
    return RT_EOK;
}
static rt_uint32_t uart_sample_get_buf(serial_t *serial ,char*recvbuf,rt_uint32_t len)
{
    int ret = 0;
    rt_sem_take(&serial->rx_sem, RT_WAITING_FOREVER);
    ret = rt_device_read(serial->serialdev, 0, recvbuf, len);
    rt_sem_control(&serial->rx_sem, RT_IPC_CMD_RESET, RT_NULL);
    return ret;
}
static char uart_sample_get_char(void)
{
    char ch;

    while (rt_device_read(serial, 0, &ch, 1) == 0)
    {
        rt_sem_control(&rx_sem, RT_IPC_CMD_RESET, RT_NULL);
        rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
    }
    return ch;
}
/* 数据解析线程 */
static void data_parsing(void *parameter)
{
    serial_t *serial = (serial_t *)parameter;
    rt_uint32_t buffer_len;
    char data[1024];
    static char i = 0;
    while (1)
    {
        buffer_len = uart_sample_get_buf(serial,data,1024);
        //rt_device_write(serial, 0, &ch, 1);
        printf("Recv Buffer:\n");
        dump_buffer(data, buffer_len);
        handle_wn_reveivedata(data, buffer_len);

    }
}

rt_uint8_t serial_open(serial_t *serial)
{
    /* 查找系统中的串口设备 */
    serial->serialdev = rt_device_find(serial->serialName);
    if (!serial->serialdev)
    {
        rt_kprintf("find %s failed!\n", serial->serialName);
        return RT_ERROR;
    }
    /* step3：控制串口设备。通过控制接口传入命令控制字，与控制参数 */
    rt_device_control(serial->serialdev, RT_DEVICE_CTRL_CONFIG, &serial->conf);

    /* 初始化信号量 */
    rt_sem_init(&serial->rx_sem, "rx_sem1", 0, RT_IPC_FLAG_FIFO);
    /* 以中断接收及轮询发送模式打开串口设备 */
    rt_device_open(serial->serialdev, RT_DEVICE_FLAG_INT_RX);
    serial->serialdev->user_data = serial;
    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(serial->serialdev, uart_rx_ind);
    /* 创建 serial 线程 */
    //rt_thread_t thread = rt_thread_create("serial", (void (*)(void *parameter))data_parsing, serial, 1024, 25, 10);
    /* 创建成功则启动线程 */
    //if (thread != RT_NULL)
    //{
    //    rt_thread_startup(thread);
    //}
    //else
    //{
    //    ret = RT_ERROR;
    //}

    return 0;
}
rt_uint8_t serial_send(serial_t *serial, rt_uint8_t *sendBuf, rt_uint32_t sendBufLen)
{
    return rt_device_write(serial->serialdev, 0, sendBuf, sendBufLen);
}
rt_uint8_t serial_close(serial_t *serial)
{

}
rt_uint8_t serial_recv(serial_t *serial, rt_uint8_t *recvBuf, uint32_t recvBufLen, uint32_t timeout, uint32_t tryOneDelayMs,uint32_t *sendBufLen)
{
    uint8_t *read_buff = recvBuf;
    uint16_t read_len = 0;
    int rc = -1;
    int wait_rc = -1;

    do
    {
        read_len += rt_device_read(serial->serialdev,0, recvBuf + read_len, recvBufLen - read_len);
        if (read_len >= recvBufLen)
        {
            return read_len; // read ok
        }
        wait_rc = rt_sem_take(&(serial->rx_sem), timeout);
        if (wait_rc < RT_EOK)
        {
            rc =  -2;
        }
        else{
            rc = 0;
        }
    } while (rc >= 0);

    return rc; // happen An error occurred
}
